<?php


namespace App\Utils\Traits;


use Symfony\Component\HttpFoundation\JsonResponse;

trait JsonTrait {
    protected function do () {
        $args = func_get_args();
        $result = $args[0];
        if ($result == 'true') {
            $json = ['result' => ['success' => $result, 'error_code' => '', 'error_msg' => '',]];
            if (func_num_args() > 1) $json['content'] = $args[1];
        } else {
            $json = ['result' => ['success' => $result, 'error_code' => $args[2], 'error_msg' => $args[1]]];
        }
        return new JsonResponse($json);
    }

    protected function jsonSuccess ($data = NULL) {
        return $this->do(true, $data);
    }

    protected function jsonFailure ($error_msg = '系统异常', $error_code = 500) {
        return $this->do(false, $error_msg, $error_code);
    }

    protected function jsonToArray ($json) {
        return json_decode($json, true);
    }
}